package org.aom.litebean.module.security;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.aom.entity.menu.Menu;
import org.aom.entity.security.User;
import org.aom.litebean.module.base.BeforeRenderBaseBean;
import org.aom.service.menu.IMenuManager;
import org.aom.service.security.IUserManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;
import org.operamasks.faces.util.Browser;

@ManagedBean(name = "module.security.userLinkMenuBean", scope = ManagedBeanScope.SESSION)
public class UserLinkMenuBean extends BeforeRenderBaseBean {

	private Long selectedModuleId;

	private User selectUser;

	public static String SELECTED_MODULE_ID = "selectedModuleId";

	@Bind(id = "uncheckedDataGrid", attribute = "binding")
	private UIDataGrid uncheckedDataGrid;

	@Bind(id = "checkedDataGrid", attribute = "binding")
	private UIDataGrid checkedDataGrid;

	@ManagedProperty("#{menuManager}")
	private IMenuManager menuManager;

	@ManagedProperty("#{userManager}")
	private IUserManager userManager;

	@Bind(id = "uncheckedDataGrid", attribute = "dataProvider")
	public GridDataProvider getUnDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return menuManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				List<Menu> un = menuManager.getAll();
				List<Menu> ck = new ArrayList<Menu>();
				ck.add(getSelectedUser().getMenu());
				un.removeAll(ck);
				return un.toArray();
			}

			public int getTotalCount() {
				return menuManager.getAll().size() - 1;
			}
		};
	}

	@Bind(id = "checkedDataGrid", attribute = "dataProvider")
	public GridDataProvider getCkDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return menuManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				return new Object[] { getSelectedUser().getMenu() };
			}

			public int getTotalCount() {
				return 1;
			}
		};
	}

	private User getSelectedUser() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
		return userManager.get(selectedModuleId);
	}

	@Action(id = "del")
	public void del() {
		selectUser = getSelectedUser();
		Object[] ms = checkedDataGrid.getSelectedValues();
		if (ms == null || ms.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			selectUser.setMenu(null);
			userManager.save(selectUser);
			uncheckedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.reload();
		}
	}

	@Action(id = "add")
	public void add() {
		selectUser = getSelectedUser();
		Object[] ms = uncheckedDataGrid.getSelectedValues();
		if (ms == null || ms.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object menu : ms) {
				selectUser.setMenu((Menu) menu);
			}
			userManager.save(selectUser);
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.setSelections(new int[] { -1 });
			uncheckedDataGrid.reload();
		}
	}

}
